package com.shanks.home.net;

import java.io.UnsupportedEncodingException;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shanks.home.R;
import com.shanks.home.model.GameData;
import com.shanks.home.module.network.SocketModule;
import com.shanks.home.util.DeviceUtil;
import com.shanks.home.util.LogUtil;

/**
 * @author Shanks
 * @time 2013.11.25
 */
public class NetworkActivity extends Activity {

	private Button connectBtn, httpConnectBtn, socketEnterRoomBtn;
	private TextView infoTV;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.activity_network);
		initView();
	}

	private void initView() {
		connectBtn = (Button)this.findViewById(R.id.network_connect_btn);
		httpConnectBtn = (Button)this.findViewById(R.id.network_http_connect_btn);
		socketEnterRoomBtn = (Button)this.findViewById(R.id.network_socket_enter_room_btn);
		infoTV = (TextView)this.findViewById(R.id.network_info_tv);

		connectBtn.setOnClickListener(onClickListener);
		httpConnectBtn.setOnClickListener(onClickListener);
		socketEnterRoomBtn.setOnClickListener(onClickListener);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int id = v.getId();
			if (id == R.id.network_connect_btn) {
				try {
					login();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else if (id == R.id.network_http_connect_btn) {
				getHttpData();
			} else if (id == R.id.network_socket_enter_room_btn) {
				enterRoom();
			}
		}
	};

	private void login() throws UnsupportedEncodingException {
		HttpService.beginPOST("https://kalaweb-a.akamaihd.net/ipokerid/mobile_java.php");
		HttpService.doNotUseDefaultParam();
		addStaticParamToHttpService();
		HttpService.addParameter("mobile_request", Base64.encodeToString(("C4:6A:B7:61:7E:C7" + "_" + "abctest").getBytes("utf-8"), Base64.DEFAULT));
		HttpService.addParameter("pay", "android");
		HttpService.setResponder(loginResponser);
		HttpService.asyncRequest();
	}

	protected static void addStaticParamToHttpService() {
		HttpService.addParameter("device", "android");
		HttpService.addParameter("version", "1.0");
		HttpService.addParameter("mac", DeviceUtil.getMacAddress());
		HttpService.addParameter("deviceId", DeviceUtil.getDeviceId());
		HttpService.addParameter("deviceName", "");
	}

	private HttpResponser loginResponser = new HttpResponser() {

		@Override
		public void onSuccess(int status, String data) throws Exception {
			try {
				infoTV.setText(data);
				JSONObject jsonObject = new JSONObject(data);
				GameData.uid = jsonObject.optInt("uid");
				GameData.mtkey = jsonObject.optString("mtkey");
				GameData.smallImgUrl = jsonObject.optString("s_picture");
				GameData.giftId = jsonObject.optInt("user_gift");
				GameData.proxyIp = jsonObject.optString("proxyip");
				GameData.proxyPort = jsonObject.optInt("proxyport");
				
				LogUtil.i("", "ip:" + GameData.proxyIp);
				LogUtil.i("", "port:" + GameData.proxyPort);
			} catch (Exception e) {
			}
		}

		@Override
		public void onFailure(Throwable t, String data) throws Exception {
			
		}
	};

	private void getHttpData() {
		HttpService.beginPOST();
		HttpService.addParameter("mod", "rank");
		HttpService.addParameter("act", "money");		
		HttpService.setResponder(httpResponser);		
		HttpService.asyncRequest();
	}

	private HttpResponser httpResponser = new HttpResponser() {

		@Override
		public void onSuccess(int status, String data) throws Exception {
			infoTV.setText(data);
		}

		@Override
		public void onFailure(Throwable t, String data) throws Exception {
		}
	};

	private void enterRoom() {
		SocketModule socketModule = new SocketModule();
		socketModule.enterRoom();
	}
	
}